#! /bin/bash

if [[ $1 == "" ]];then
   DT=`date -d '1 days ago' "+%Y-%m-%d"`
else
   DT=$1
fi

# 先删除dt分区
${HIVE_HOME} -S -e "
   ALTER TABLE edu_ods.class_studying_student_count DROP IF EXISTS PARTITION (dt='${DT}');
   ALTER TABLE edu_ods.course_table_upload_detail DROP IF EXISTS PARTITION (dt='${DT}');
   ALTER TABLE edu_ods.student_leave_apply DROP IF EXISTS PARTITION (dt='${DT}');
   ALTER TABLE edu_ods.tbh_class_time_table DROP IF EXISTS PARTITION (dt='${DT}');
   ALTER TABLE edu_ods.tbh_student_signin_record DROP IF EXISTS PARTITION (dt='${DT}');
"
echo '========================================'
echo '==============开始全量导入==============='
echo '========================================'

/usr/bin/sqoop import "-Dorg.apache.sqoop.splitter.allow_text_splitter=true" \
--connect 'jdbc:mysql://106.75.33.59:3306/teach?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true' \
--username itcast_edu_stu \
--password itcast_edu_stu \
--query "select * from student_leave_apply where 1=1 and \$CONDITIONS" \
--hcatalog-database edu_ods \
--hcatalog-table student_leave_apply \
-m 1

echo '========================================'
echo '==============开始增量导入==============='
echo '========================================'

/usr/bin/sqoop import "-Dorg.apache.sqoop.splitter.allow_text_splitter=true" \
--connect 'jdbc:mysql://106.75.33.59:3306/teach?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true' \
--username itcast_edu_stu \
--password itcast_edu_stu \
--query "select *,'${DT}' as dt from class_studying_student_count where 1=1 and studying_date between '${DT} 00:00:00' and '${DT} 23:59:59' and  \$CONDITIONS" \
--hcatalog-database edu_ods \
--hcatalog-table class_studying_student_count \
-m 1

/usr/bin/sqoop import "-Dorg.apache.sqoop.splitter.allow_text_splitter=true" \
--connect 'jdbc:mysql://106.75.33.59:3306/teach?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true' \
--username itcast_edu_stu \
--password itcast_edu_stu \
--query "select *,'${DT}' as dt from course_table_upload_detail where 1=1 and class_date = '${DT}' and  \$CONDITIONS" \
--hcatalog-database edu_ods \
--hcatalog-table course_table_upload_detail \
-m 1

/usr/bin/sqoop import "-Dorg.apache.sqoop.splitter.allow_text_splitter=true" \
--connect 'jdbc:mysql://106.75.33.59:3306/teach?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true' \
--username itcast_edu_stu \
--password itcast_edu_stu \
--query "select *,'${DT}' as dt from tbh_class_time_table where 1=1 and create_time between '${DT} 00:00:00' and '${DT} 23:59:59' and  \$CONDITIONS" \
--hcatalog-database edu_ods \
--hcatalog-table tbh_class_time_table \
-m 1

/usr/bin/sqoop import "-Dorg.apache.sqoop.splitter.allow_text_splitter=true" \
--connect 'jdbc:mysql://106.75.33.59:3306/teach?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true' \
--username itcast_edu_stu \
--password itcast_edu_stu \
--query "select *,'${DT}' as dt from tbh_student_signin_record where 1=1 and signin_date between '${DT} 00:00:00' and '${DT} 23:59:59' and  \$CONDITIONS" \
--hcatalog-database edu_ods \
--hcatalog-table tbh_student_signin_record \
-m 1

echo '========================================'
echo '=================success==============='
echo '=========================='
